package com.qysoft.dtkj.dtkj_gl.actions.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.qysoft.dtkj.dtkj_gl.utils.IOUtil;
import com.qysoft.rapid.actions.JsonResultInterceptor;
import com.qysoft.rapid.actions.mybatis.BaseCommonAction;
import com.qysoft.rapid.core.RapidDbConnTx;
import com.qysoft.rapid.dao.mybatis.RapidDao;
import com.qysoft.rapid.exceptions.RapidException;

public class CommonAction extends BaseCommonAction {

	@Override
	public void handlerPar(HashMap<String, Object> parameters) {
		bindCurrentRyxxToMap(parameters);
	}

	@Override
	public String rendererCellData(String sqlid, String dataIndex,
			String value) {
		return null;
	} 
	
	@Clear(JsonResultInterceptor.class)
	public void main(){ 
		renderJsp("/WEB-INF/pages/common/main.jsp");
	}
	
	@Before(value={JsonResultInterceptor.class,RapidDbConnTx.class})
	public void exportWord() throws Exception{
		String id = getPara("id");
		HashMap<String, Object> infoDetail = RapidDao.selectOneByXml("infoDetail_select", id);
		String content = (String) infoDetail.get("CONTENT");
		String wordPath = getSession().getServletContext().getRealPath("/")+"/tmp/";
		String wordName = UUID.randomUUID().toString()+".doc";
		IOUtil.wirteFile(content.getBytes(), wordPath+wordName);
		setJson(wordName);
	}
	
	/**
	 * 文件下载
	 */
	public void downLoadWord(){
	   String excelPath = getSession().getServletContext().getRealPath("/")+"/tmp/";
	   HttpServletResponse response = getResponse();
	   String tartgetName = getPara("targetName");
	   String wordName = getPara("wordName");
	   File file = null;
	   try{
		   file = new File(excelPath+tartgetName);
		   String fileName=file.getName();
		   InputStream is=new FileInputStream(file);
		   OutputStream os=response.getOutputStream();
		   BufferedInputStream bis = new BufferedInputStream(is);
		   BufferedOutputStream bos = new BufferedOutputStream(os);
		   wordName = java.net.URLEncoder.encode(wordName, "UTF-8");// 处理中文文件名的问题
		   wordName = new String(wordName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
		   response.reset();
		   response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型
		   if(wordName==null||"".equals(wordName))
		   {
			   response.setHeader("Content-Disposition", "attachment; filename="+fileName);
		   }else{
			   wordName = wordName+".doc";
			   response.setHeader("Content-Disposition", "attachment; filename="+wordName);
		   }
		   int bytesRead = 0;
		   byte[] buffer = new byte[1024];
		   while ((bytesRead = bis.read(buffer)) != -1){
		   bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
		}
		bos.flush();
		bos.close();
		bis.close();
		is.close();
		os.close();
		bos=null;
		bis=null;
		is=null;
		os=null;  
		}catch(Exception e){
			throw new RapidException(e.getMessage());
		}finally{
			 file.delete();
		}
   }	

}
